package com.tc.zookeeper;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;

public class MatchUtils {
	private static final Logger LOGGER = LoggerFactory.getLogger(MatchUtils.class);

	//根据matcher匹配zk ip,如果没有匹配的ip，则返回原始zk ip
	public static String useMatcher(String connectString, String isUseWeightStragety) {
		String target = "";
		boolean isUseMatcher = true;//默认开启同机房优先策略
		if (StringUtils.isNotEmpty(isUseWeightStragety)) {
			isUseMatcher = Boolean.parseBoolean(isUseWeightStragety);
		}
      
		if (isUseMatcher) {    
			String ipPattern = "";
			try {
				ipPattern = ZKUtils.resolveSystemProperty(ZKConstants.VIP_CFGCENTER_CONNECTION_MATCHER);
				if(StringUtils.isBlank(ipPattern)){
					String myIp = InetAddress.getLocalHost().getHostAddress();
					if (StringUtils.isNotBlank(myIp)) {
						String[] ipArray = StringUtils.split(myIp, ZKConstants.DOT);
						ipPattern = ipArray[0] + ZKConstants.DOT + ipArray[1] + ZKConstants.DOT + "**";//如果不配置matcher，默认根据前两个ip匹配
					}
				}		
			} catch (Exception e) {
				LOGGER.error("useDefaultMatcher, get localhost error." + e.getMessage());
			}

			target = matchPattern(connectString, ipPattern);
		}

		if (StringUtils.isNotBlank(target)) {
			return target;
		}

		return connectString;
	}

	public static String matchPattern(String connectString, String matchPattern) {
		StringBuilder targetBuilder = new StringBuilder();
		String[] zkConnectStrings = StringUtils.split(connectString, ZKConstants.COMMA);
		for (final String zkConnectString : zkConnectStrings) {
			if (ZKUtils.isMatch(matchPattern, zkConnectString)) {
				targetBuilder.append(zkConnectString + ZKConstants.COMMA);
			}
		}

		return StringUtils.removeEnd(targetBuilder.toString(), ZKConstants.COMMA);
	}

	public static String getConnectStringByWeightStragety(String connectString) {
		String isUseWeightStragety = ZKUtils.resolveSystemProperty(ZKConstants.VIP_CFGCENTER_ZK_USEWEIGHTSTRATEGY);
		String filterConnectString = useMatcher(connectString,isUseWeightStragety);
		if(!filterConnectString.equals(connectString)){
			return  makeConnectStringByWeight(connectString,filterConnectString);
		}
		return connectString;
	}
	
  public static String makeConnectStringByWeight(String connectString,String filterConnectString){
	    Integer weight = ZKConstants.VIP_CFGCENTER_ZK_DEFAULT_WEIGHT;//默认权重系数为3
	    String weightPara = ZKUtils.resolveSystemProperty(ZKConstants.VIP_CFGCENTER_ZK_WEIGHT);
	    if(StringUtils.isNotBlank(weightPara)){
	    	try{
	    		weight = Integer.parseInt(weightPara);
	    	}catch(Exception e){
	    		LOGGER.error("weight parameter must be a number.");
	    		weight = ZKConstants.VIP_CFGCENTER_ZK_DEFAULT_WEIGHT;
	    	}
	    	if(weight > 100 || weight < 0 || weight == 0){
	    		LOGGER.error("weight parameter must be >0 and <= 100");
	    		weight = ZKConstants.VIP_CFGCENTER_ZK_DEFAULT_WEIGHT;
    		}
	    }
		StringBuilder sb = new StringBuilder();
		sb.append(connectString);
		for(int i=0;i<weight-1;i++){
			sb.append(ZKConstants.COMMA);
			sb.append(filterConnectString);
		}
		return sb.toString();
  }
}
